package com.youku.core.view.components
{
   import flash.display.Sprite;
   import com.youku.interfaces.ICore;
   import com.youku.interfaces.IPlayListData;
   import com.youku.interfaces.IPlayerProxy;
   import flash.display.BitmapData;
   import flash.display.Bitmap;
   import com.youku.PlayerConfig;
   import com.youku.events.CoreEvent;
   import com.youku.core.view.interfaces.IVideo;
   import com.youku.P2PConfig;
   import com.youku.utils.FlashVersionCheck;
   import com.youku.data.PlayerConstant;
   import flash.filters.ColorMatrixFilter;
   import flash.filters.BitmapFilter;
   import flash.events.Event;
   import com.youku.utils.DropFrameTable;
   import com.youku.interfaces.IVideoSegmentStatus;
   import flash.display.DisplayObject;
   
   public class CoreBase extends Sprite implements ICore
   {
      
      public function CoreBase(param1:IPlayerProxy) {
         this._bitmap = new Bitmap();
         super();
         this._playerProxy = param1;
      }
      
      public static var VideoUrlArr:Array = [];
      
      public static var VideoSecondArr:Array = [];
      
      public static var VideoSizeArr:Array = [];
      
      private var _width:Number = 100;
      
      private var _height:Number = 100;
      
      private var _background:Sprite;
      
      private var _base:Sprite;
      
      private var _frontground:Sprite;
      
      private var _playListData:IPlayListData;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _storeVolume:Number;
      
      private var _tempVolume:Number;
      
      private var _contrast:Number = 0;
      
      private var _bright:Number = 0;
      
      private var _saturation:Number = 0;
      
      private var _core:BaseCore;
      
      private var _closed:Boolean = false;
      
      private var _bitmapdata:BitmapData;
      
      private var _bitmap:Bitmap;
      
      private var _useRealSizeBitmap:Boolean = true;
      
      private var _loadedBytes:Number = 0;
      
      private var _lastLoadedBytes:Number = 0;
      
      private var _baseLoadedBytes:Number = 0;
      
      public function init(param1:Number, param2:Number) : void {
         this._width = param1;
         this._height = param2;
         this._playListData = PlayerConfig.playListData;
         this._core = this.createCore();
         this._closed = false;
         this.createBack();
         this.createBase();
         this.createFront();
         this.addChild(this._background);
         this.addChild(this._base);
         this.addChild(this._frontground);
         this._base.addChild(this._core);
         this.addEvents();
      }
      
      private function addEvents() : void {
         this.addEventListener(CoreEvent.GOT_METADATA,this.onGotMetaData);
      }
      
      private function onGotMetaData(param1:CoreEvent) : void {
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         var _loc2_:IVideo = param1.data.video;
         if(_loc2_)
         {
            _loc3_ = this._useRealSizeBitmap?_loc2_.videoRealWidth:this._width;
            _loc4_ = this._useRealSizeBitmap?_loc2_.videoRealHeight:this._height;
            this._bitmapdata = new BitmapData(_loc3_,_loc4_,false,0);
            this._bitmap.bitmapData = this._bitmapdata;
         }
         this._playerProxy.dispatchEvent(new CoreEvent(param1.type,param1.data));
      }
      
      public function startToPlay() : void {
         this._core.startToPlay();
      }
      
      public function reInit() : void {
         if(!(this._core is OneNSCore))
         {
            P2PConfig.P2PStatus = P2PConfig.SOCKET_FAIL_ERROR;
         }
         this._playListData = PlayerConfig.playListData;
         this._core.reInit();
         this._closed = false;
      }
      
      private function createCore() : BaseCore {
         var _loc1_:BaseCore = new MultiNSCore(this._playerProxy,this);
         var _loc2_:* = "noP2P";
         if((P2PConfig.isP2PRunning) && !this._playerProxy.playerData.isIKuInstall && !PlayerConfig.isRTMP && !this.playListData.drm && (FlashVersionCheck.checkVersionForCapab()) && !this._playerProxy.playerData.threedInfo)
         {
            _loc1_ = new OneNSCore(this._playerProxy,this);
            if(P2PConfig.isYKUseP2P)
            {
               _loc2_ = "P2P";
            }
            else
            {
               _loc2_ = "P2P_Nouse";
            }
         }
         else
         {
            _loc2_ = "p2pError_";
            if(!P2PConfig.isP2PRunning)
            {
               _loc2_ = _loc2_ + P2PConfig.P2PStatus;
            }
            else if(this._playerProxy.playerData.isIKuInstall)
            {
               _loc2_ = _loc2_ + "ikuInstall";
            }
            else if(PlayerConfig.isRTMP)
            {
               _loc2_ = _loc2_ + "isRTMP";
            }
            else if(this.playListData.drm)
            {
               _loc2_ = _loc2_ + "isDRM";
            }
            else if(!FlashVersionCheck.checkVersionForCapab())
            {
               _loc2_ = _loc2_ + "flashVersionLow";
            }
            else if(this._playerProxy.playerData.threedInfo)
            {
               _loc2_ = _loc2_ + "isThreed";
            }
            else
            {
               _loc2_ = _loc2_ + "other";
            }
            
            
            
            
            
            P2PConfig.P2PStatus = P2PConfig.SOCKET_FAIL_ERROR;
         }
         PlayerConfig.p2pStatus = _loc2_;
         _loc1_.init(this._width,this._height);
         return _loc1_;
      }
      
      public function tryToUseMultyCore(param1:Number) : void {
         if(this._base.contains(this._core))
         {
            this._base.removeChild(this._core);
         }
         this._core.close();
         P2PConfig.P2PStatus = P2PConfig.SOCKET_FAIL_ERROR;
         this._core = new MultiNSCore(this._playerProxy,this);
         this._core.init(this._width,this._height);
         this._core.volume = this._storeVolume;
         this._base.addChild(this._core);
         this._core.startToPlay();
         this._core.seek(param1);
      }
      
      private function createBack() : void {
         this._background = new Sprite();
         this.drawBack();
      }
      
      private function createBase() : void {
         this._base = new Sprite();
      }
      
      private function createFront() : void {
         this._frontground = new Sprite();
      }
      
      private function drawBack() : void {
         var _loc1_:uint = 0;
         if(PlayerConfig.specialType == PlayerConstant.SPECIAL_TYPE_SHENHE)
         {
            _loc1_ = 26265;
         }
         this._background.graphics.clear();
         this._background.graphics.beginFill(_loc1_);
         this._background.graphics.drawRect(0,0,this._width,this._height);
         this._background.graphics.endFill();
      }
      
      public function setAdjustParameter(param1:Number, param2:Number, param3:Number) : void {
         this._contrast = param1;
         this._bright = param2;
         this._saturation = param3;
         var _loc4_:Array = new Array();
         _loc4_ = _loc4_.concat([1,0,0,0,0]);
         _loc4_ = _loc4_.concat([0,1,0,0,0]);
         _loc4_ = _loc4_.concat([0,0,1,0,0]);
         _loc4_ = _loc4_.concat([0,0,0,1,0]);
         var _loc5_:* = 0;
         _loc5_ = 0;
         while(_loc5_ < 15)
         {
            if(_loc5_ % 5 < 3)
            {
               _loc4_[_loc5_] = Number(_loc4_[_loc5_]) + this._saturation * -0.3;
            }
            _loc5_++;
         }
         _loc4_[0] = Number(_loc4_[0]) - 3 * this._saturation * -0.3;
         _loc4_[6] = Number(_loc4_[6]) - 3 * this._saturation * -0.3;
         _loc4_[12] = Number(_loc4_[12]) - 3 * this._saturation * -0.3;
         _loc5_ = 0;
         while(_loc5_ < 15)
         {
            if(_loc5_ % 5 == 4)
            {
               _loc4_[_loc5_] = Number(_loc4_[_loc5_]) + 100 * param2;
            }
            _loc5_++;
         }
         _loc5_ = 0;
         while(_loc5_ < 15)
         {
            if(_loc5_ % 5 == 4)
            {
               _loc4_[_loc5_] = Number(_loc4_[_loc5_]) - 128 * this._contrast * 0.7;
            }
            _loc5_++;
         }
         _loc4_[0] = Number(_loc4_[0]) + 1 * this._contrast * 0.7;
         _loc4_[6] = Number(_loc4_[6]) + 1 * this._contrast * 0.7;
         _loc4_[12] = Number(_loc4_[12]) + 1 * this._contrast * 0.7;
         var _loc6_:BitmapFilter = new ColorMatrixFilter(_loc4_);
         this.filters = [_loc6_];
      }
      
      public function seek(param1:Number) : void {
         this._core.seek(param1);
      }
      
      public function mockSeek(param1:Number) : void {
         this._core.mockSeek(param1);
      }
      
      public function play(param1:Number = 0) : void {
         this._core.play(param1);
      }
      
      public function playVideoByID(param1:String, param2:Boolean = true) : void {
         var _loc3_:CoreEvent = new CoreEvent(CoreEvent.PLAY_VIDEO_BY_ID,{"videoID":param1});
         this.dispatchEvent(_loc3_);
         if(param2)
         {
            this.dispatchEvent(new CoreEvent(CoreEvent.AUTO_PLAYED));
         }
      }
      
      public function pause(param1:Boolean = false) : void {
         this._core.pause(param1);
      }
      
      public function resume() : void {
         this._core.resume();
      }
      
      public function togglePausePlay(param1:Boolean = false) : void {
         this._core.togglePausePlay(param1);
      }
      
      public function playWaitLoad(param1:Number = 0) : void {
         this._core.playWaitLoad(param1);
      }
      
      public function close() : void {
         this._closed = true;
         this._core.close();
         this.removeEventListener(Event.ENTER_FRAME,this.onEnterFrame);
      }
      
      public function setScreenMode(param1:String, param2:Boolean, param3:Boolean) : void {
         this._core.setScreenMode(param1,param2);
         if(param3)
         {
            dispatchEvent(new CoreEvent(CoreEvent.SCREEN_MODE_CHANGED,{"mode":param1}));
         }
      }
      
      public function setQuality(param1:String) : void {
         if(param1 == "low")
         {
            param1 = "flv";
         }
         else if(param1 == "mid")
         {
            param1 = "mp4";
         }
         else if(param1 == "high")
         {
            param1 = "hd2";
         }
         else if(param1 == "1080P")
         {
            param1 = "hd3";
         }
         else if(param1 == "auto")
         {
            param1 = this.chooseAutoQuality();
         }
         
         
         
         
         var param1:String = this.chooseNextHighQuality(param1);
         this._core.setQuality(param1);
         dispatchEvent(new CoreEvent(CoreEvent.QUALITY_CHANGED,{"quality":param1}));
      }
      
      public function setWH(param1:Number, param2:Number) : void {
         this._width = param1;
         this._height = param2;
         this.drawBack();
         this._core.setWH(param1,param2);
      }
      
      public function setAngle(param1:Number) : void {
         this._core.setAngle(param1);
      }
      
      public function setDragging(param1:Boolean) : void {
         this._core.setDragging(param1);
      }
      
      public function refreshLoaded() : void {
      }
      
      public function getNsTime() : Number {
         return this._core.time;
      }
      
      public function resetData() : void {
         this._core.resetData();
      }
      
      public function get isPause() : Boolean {
         return this._core.isPause;
      }
      
      public function get loadedTime() : Number {
         return this._core.loadedTime;
      }
      
      public function get droppedFrames() : Number {
         return this._core.dfTable.droppedFrames;
      }
      
      public function get dropFrameTable() : DropFrameTable {
         return this._core.dfTable;
      }
      
      public function get playListData() : IPlayListData {
         return this._playListData;
      }
      
      public function getTimeStatus(param1:Number, param2:String = "") : Object {
         return this._core.getTimeStatus(param1,param2);
      }
      
      public function getVideoSegmentStatus(param1:Number) : IVideoSegmentStatus {
         return this._core.getVideoSegmentStatus(param1);
      }
      
      public function get currentVideoNum() : Number {
         if(this._core.currentPlayVideo)
         {
            return this._core.currentPlayVideo.num;
         }
         return 0;
      }
      
      public function get currentLoadingNum() : Number {
         if(this._core)
         {
            return this._core.currentLoadingNum;
         }
         return 0;
      }
      
      public function get currentFileId() : String {
         if(this._core.currentPlayVideo)
         {
            return this._core.currentPlayVideo.fileid;
         }
         return "";
      }
      
      public function get currentNsTime() : Number {
         if(this._core.currentPlayVideo)
         {
            if(this._core is OneNSCore)
            {
               return this._core.currentPlayVideo.fileidTime;
            }
            return this._core.currentPlayVideo.nsTime;
         }
         return 0;
      }
      
      public function get currentLoadedTime() : Number {
         if(this._core.currentPlayVideo)
         {
            return this._core.currentPlayVideo.loadedTime;
         }
         return 0;
      }
      
      public function get currentTotalTime() : Number {
         if(this._core.currentPlayVideo)
         {
            return this._core.currentPlayVideo.totalTime;
         }
         return 0;
      }
      
      public function get currentVideoWidth() : Number {
         if(this._core.currentPlayVideo)
         {
            return this._core.video.videoWidth;
         }
         return 0;
      }
      
      public function get currentVideoHeight() : Number {
         if(this._core.currentPlayVideo)
         {
            return this._core.video.videoHeight;
         }
         return 0;
      }
      
      public function get currentVideoRealWidth() : Number {
         if(this._core.currentPlayVideo)
         {
            return this._core.currentPlayVideo.videoRealWidth;
         }
         return 0;
      }
      
      public function get currentVideoRealHeight() : Number {
         if(this._core.currentPlayVideo)
         {
            return this._core.currentPlayVideo.videoRealHeight;
         }
         return 0;
      }
      
      public function get video() : DisplayObject {
         return this._bitmap;
      }
      
      public function get volume() : Number {
         return this._storeVolume;
      }
      
      public function jumpTail() : void {
         this._core.jumpTail();
      }
      
      public function saveWatchingRecord(param1:Boolean = false) : void {
         this._core.saveWatchingRecord(param1);
      }
      
      public function stopSizeRender(param1:Boolean = true) : void {
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         this._useRealSizeBitmap = param1;
         this._core.stopSizeRender(param1);
         var _loc2_:IVideo = this._core.currentPlayVideo;
         if(_loc2_)
         {
            _loc3_ = this._useRealSizeBitmap?_loc2_.videoRealWidth:this._width;
            _loc4_ = this._useRealSizeBitmap?_loc2_.videoRealHeight:this._height;
            this._bitmapdata = new BitmapData(_loc3_,_loc4_,false,0);
            this._bitmap.bitmapData = this._bitmapdata;
         }
         this.addEventListener(Event.ENTER_FRAME,this.onEnterFrame);
      }
      
      private function onEnterFrame(param1:Event) : void {
         var e:Event = param1;
         if(!this._bitmapdata)
         {
            return;
         }
         var video:DisplayObject = this._core as DisplayObject;
         if(video)
         {
            try
            {
               this._bitmapdata.draw(video);
               this._bitmap.bitmapData = this._bitmapdata;
               this._bitmap.smoothing = true;
            }
            catch(e:Error)
            {
            }
         }
      }
      
      public function set volume(param1:Number) : void {
         if(param1 < 0)
         {
            param1 = 0;
         }
         this._storeVolume = param1;
         this._core.volume = this._storeVolume;
      }
      
      private function chooseAutoQuality() : String {
         var _loc1_:int = this.playListData.controller.stream_mode;
         var _loc2_:* = "flv";
         if(_loc1_ == 1)
         {
            _loc2_ = "flv";
         }
         else if(_loc1_ == 2)
         {
            _loc2_ = "mp4";
         }
         else if(_loc1_ == 3)
         {
            _loc2_ = "hd2";
         }
         
         
         var _loc3_:* = false;
         var _loc4_:* = false;
         var _loc5_:* = 0;
         while(_loc5_ < this.playListData.typeArr.length)
         {
            if(this.playListData.typeArr[_loc5_] == "hd2")
            {
               _loc3_ = true;
            }
            if(this.playListData.typeArr[_loc5_] == "mp4")
            {
               _loc4_ = true;
            }
            _loc5_++;
         }
         if(_loc2_ == "mp4" && !_loc4_)
         {
            _loc2_ = "flv";
         }
         if(_loc2_ == "hd2" && !_loc3_)
         {
            _loc2_ = "flv";
         }
         return _loc2_;
      }
      
      public function get baseLoadedBytes() : Number {
         return this._baseLoadedBytes;
      }
      
      public function get diffLoadedBytes() : Number {
         var _loc1_:Number = this._loadedBytes - this._lastLoadedBytes;
         this._lastLoadedBytes = this._loadedBytes;
         return _loc1_;
      }
      
      public function get loadedBytes() : Number {
         this._lastLoadedBytes = this._loadedBytes;
         return this._loadedBytes;
      }
      
      public function set loadedBytes(param1:Number) : void {
         this._loadedBytes = param1;
      }
      
      public function get closed() : Boolean {
         return this._closed;
      }
      
      private function chooseNextHighQuality(param1:String) : String {
         var _loc2_:* = "";
         if(param1 == "hd3")
         {
            if(this._playListData.has1080P)
            {
               _loc2_ = "hd3";
            }
            else if(this._playListData.hasHD)
            {
               _loc2_ = "hd2";
            }
            else if(this._playListData.hasMid)
            {
               _loc2_ = "mp4";
            }
            else
            {
               _loc2_ = "flv";
            }
            
            
         }
         else if(param1 == "hd2")
         {
            if(this._playListData.hasHD)
            {
               _loc2_ = "hd2";
            }
            else if(this._playListData.hasMid)
            {
               _loc2_ = "mp4";
            }
            else
            {
               _loc2_ = "flv";
            }
            
         }
         else if(param1 == "mp4")
         {
            if(this._playListData.hasMid)
            {
               _loc2_ = "mp4";
            }
            else
            {
               _loc2_ = "flv";
            }
         }
         else
         {
            _loc2_ = "flv";
         }
         
         
         return _loc2_;
      }
      
      public function removeOverlays() : void {
         if(this.contains(this._background))
         {
            this.removeChild(this._background);
         }
         if(this.contains(this._base))
         {
            this.removeChild(this._base);
         }
         if(this.contains(this._frontground))
         {
            this.removeChild(this._frontground);
         }
      }
      
      public function addOverlays() : void {
         if(!this.contains(this._background))
         {
            this.addChild(this._background);
         }
         if(!this.contains(this._base))
         {
            this.addChild(this._base);
         }
         if(!this.contains(this._frontground))
         {
            this.addChild(this._frontground);
         }
      }
   }
}
